# 高可用配置
---> Redis (Replica)
client ---> Redis (Master)
---> Redis (Replica)
# 當Master掛掉時,會把Replica切換為Master提供用戶端服務
Client -----------------------> Redis (Master)
Client | 同步
Redis (X) V
Redis (Replica)
讀取/寫入
Client -----------------------> Redis (Master)
| 同步
V
Client -----------------------> Redis (Replica)
讀取only
# 高可用配置
---> Redis (Replica)
client ---> Redis (Master)
---> Redis (Replica)
# 高可用鏈結配置(應用於分散多個資料中心架構或是允許較慢同步的需求)
client ---> Redis (Master) ---> Redis (Replica) ---> Redis (Replica)
_____________________ _________
A區資料中心 (LAN) (WAN) B區資料中心
or
應用服務 報表服務
在配置Redis Server高可用架構時,基本上會配置三台伺服器,並給予相同或是不同的Port來配置,下面實際配置的內容會在單機進行,故會將Port分配三個來模擬三台伺服器的狀況.
因需要在同一台機器模擬三台伺服器的配置,故有以下前置作業需要先完成 (如果三台伺服器則不需要這樣做)
# 複製三份設定檔,結尾用Port命名
$ cp /home/redis/config/redis.conf redis_6379.conf
$ cp /home/redis/config/redis.conf redis_6380.conf
$ sudo cp /home/redis/config/redis.conf redis_6381.conf
# 修改各個設定檔中的rdb與aof檔案名稱,一樣結尾用Port命名
# redis_6379.conf
vi dump_6379.rdb
dbfilename dump_6379.rdb
appendfilename "appendonly_6379.aof"
# redis_6380.conf
vi dump_6380.rdb
dbfilename dump_6380.rdb
appendfilename "appendonly_6380.aof"
# redis_6381.conf
vi dump_6381.rdb
dbfilename dump_6381.rdb
appendfilename "appendonly_6381.aof"
# 啟動服務
./redis-server ./redis_6379.conf
./redis-server ./redis_6380.conf
./redis-server ./redis_6381.conf
#連線到 127.0.0.1 Port: 6379
./redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:0
master_failover_state:no-failover
master_replid:1363014961ee0ae4061c2493c44a709b387c0c5e
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
#連線到 127.0.0.1 Port: 6380
./redis-cli -h 127.0.0.1 -p 6380
127.0.0.1:6380> info replication
# Replication
role:master
connected_slaves:0
master_failover_state:no-failover
master_replid:7b48682e1628f579bf5a3c79420e0324c859046e
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
#連線到 127.0.0.1 Port: 6381
./redis-cli -h 127.0.0.1 -p 6381
127.0.0.1:6381> info replication
# Replication
role:master
connected_slaves:0
master_failover_state:no-failover
master_replid:bf1bb9f71dd3b8a4fc2784f71c700ffbb5b9c800
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
# 將127.0.0.1 Port: 6380 與 127.0.0.1 Port: 6381 設為Replica
# 如果有設定replica password 則需要在replicaof 127.0.0.1 6379 -a password
./redis-cli -h 127.0.0.1 -p 6380
127.0.0.1:6380> replicaof 127.0.0.1 6379
OK
127.0.0.1:6380> role
1) "slave"
2) "127.0.0.1"
3) (integer) 6379
4) "connected"
5) (integer) 84
./redis-cli -h 127.0.0.1 -p 6381
127.0.0.1:6381> replicaof 127.0.0.1 6379
OK
127.0.0.1:6381> role
1) "slave"
2) "127.0.0.1"
3) (integer) 6379
4) "connected"
5) (integer) 0
# 確認Replication結果
./redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> role
1) "master"
2) (integer) 308
3) 1) 1) "127.0.0.1"
2) "6381"
3) "308"
2) 1) "127.0.0.1"
2) "6380"
3) "308"
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6381,state=online,offset=336,lag=1
slave1:ip=127.0.0.1,port=6380,state=online,offset=336,lag=1
master_failover_state:no-failover
master_replid:a30878092cdd31e404af76eaa7a2d024c8ae843e
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:336
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:336
# 如果Replica要退出則可以透過以下方式
./redis-cli -h 127.0.0.1 -p 6381
127.0.0.1:6381> replicaof no one
# 在Master操作驗證同步到另外兩個Replica節點
./redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> set book a
OK
127.0.0.1:6379> get book
"a"
127.0.0.1:6379> set book b
OK
127.0.0.1:6379> get book
"b"
# 確認Replica
./redis-cli -h 127.0.0.1 -p 6380
127.0.0.1:6380> get book
"b"
# 確認Replica
./redis-cli -h 127.0.0.1 -p 6381
127.0.0.1:6381> get book
"b"
# 嘗試寫入Replica會有錯誤
127.0.0.1:6381> set book c
#(error) READONLY You can't write against a read only replica.
# 以上錯誤是因為這個設定而來的,當然也可以關閉但不建議
127.0.0.1:6381> config get replica-read-only
1) "replica-read-only"
2) "yes"